package com.cloudkinto.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author long
 * @Description redisson配置
 * @Date 2025/6/4 17:04
 */
@Configuration
public class RedissonClientConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;


    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setCodec(new StringCodec());
        config.useSingleServer()
                .setAddress(String.format("redis://%s:%s", host, port))
                .setConnectionPoolSize(64)
                .setConnectionMinimumIdleSize(10)
                .setDatabase(database)
                .setTimeout(3000);
        if (password != null && !password.isEmpty()) {
            config.useSingleServer().setPassword(password);
        }
        //序列化
        JsonJacksonCodec codec = new JsonJacksonCodec();
        config.setCodec(codec);
        return Redisson.create(config);
    }
}
